package com.x.erp.util;

import com.alibaba.fastjson.JSONObject;
import com.x.erp.thirdparty.SouHuCarInfo;
import com.x.framework.remote.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Component
public class SouHuApi {

	private Logger logger = LoggerFactory.getLogger(SouHuApi.class);

	@Autowired
	private HttpClient httpClient;

	/**
	 * 添加或修改车源信息
	 * @param souHuCarInfo 车辆信息
	 * @return
	 * @throws Exception
	 */
	public SouHuResult insertOrUpdateCar(SouHuCarInfo souHuCarInfo, String carId, Integer state) throws Exception{
		logger.info(Constant.SOUHU_URL + "?auth=" + Constant.SOUHU_AUTH + "&carid=" + carId +"&state=" + state + "&info=" + souHuCarInfo.getInfo());
		String result = httpClient.doGet(Constant.SOUHU_URL + "?auth=" + Constant.SOUHU_AUTH + "&carid=" + carId +"&state=" + state + "&info=" + URLEncoder.encode(souHuCarInfo.getInfo(), "GBK"));
		if ("".equals(result)){
			return null;
		}
		return JSONObject.parseObject(result, SouHuResult.class);
	}

	/**
	 * 获取返回信息
	 * @return
	 */
	private Map<String, String> errorMsg = new HashMap<String, String>();
	public SouHuApi(){
		errorMsg.put("0","成功");
		errorMsg.put("1","车源图片抓取失败，原始图片url");
		errorMsg.put("2","数据格式不正确");
		errorMsg.put("3","数据格式不正确");
		errorMsg.put("4","授权码不正确");
		errorMsg.put("5","未知错误");
		errorMsg.put("6","车源账号错误");
		errorMsg.put("7","车源已被禁止使用，请使用新的数据");
		errorMsg.put("8","已出售");

	}

	public String getErrorMsg(String code) {
		return errorMsg.get(code);
	}
}
